﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ShareCode.Util.Extension
{
    public static class DictionaryExtension
    {
        public static bool HasInt(this Dictionary<string, string> pars, string key)
        {
            if (pars == default)
            {
                return false;
            }

            if (pars.TryGetValue(key, out string? value) && int.TryParse(value, out _))
            {
                return true;
            }

            return false;
        }

        public static bool HasLong(this Dictionary<string, string> pars, string key)
        {
            if (pars == default)
            {
                return false;
            }

            if (pars.TryGetValue(key, out string? value) && long.TryParse(value, out _))
            {
                return true;
            }

            return false;
        }
        public static bool HasString(this Dictionary<string, string> pars, string key)
        {
            if (pars == default)
            {
                return false;
            }

            if (pars.TryGetValue(key, out string? value) && !string.IsNullOrEmpty(value))
            {
                return true;
            }

            return false;
        }

        public static int GetInt(this Dictionary<string, string> pars, string key)
        {
            if (pars == default)
            {
                return 0;
            }

            if (pars.TryGetValue(key, out string? value) && int.TryParse(value, out int v2))
            {
                return v2;
            }

            return 0;
        }

        public static long GetLong(this Dictionary<string, string> pars, string key)
        {
            if (pars == default)
            {
                return 0;
            }


            if (pars.TryGetValue(key, out string? value) && long.TryParse(value, out long v2))
            {
                return v2;
            }

            return 0;
        }

        //这个好像不用，但为了统一，还是留着吧
        public static string? GetString(this Dictionary<string, string> pars, string key)
        {
            if (pars == default)
            {
                return null;
            }

            if (pars.TryGetValue(key, out string? value))
            {
                return value;
            }

            return null;
        }

        #region  这一组好像不需要
        public static bool Eq(this Dictionary<string, string> pars, string key, int i)
        {
            if (pars == default)
            {
                return false;
            }

            return pars.TryGetValue(key, out string? value) && int.TryParse(value, out int v2) && v2 == i;
        }
        public static bool Gt(this Dictionary<string, string> pars, string key, int i)
        {
            if (pars == default)
            {
                return false;
            }

            return pars.TryGetValue(key, out string? value) && int.TryParse(value, out int v2) && v2 > i;
        }

        public static bool Gte(this Dictionary<string, string> pars, string key, int i)
        {
            if (pars == default)
            {
                return false;
            }

            return pars.TryGetValue(key, out string? value) && int.TryParse(value, out int v2) && v2 >= i;
        }

        public static bool Lt(this Dictionary<string, string> pars, string key, int i)
        {
            if (pars == default)
            {
                return false;
            }

            return pars.TryGetValue(key, out string? value) && int.TryParse(value, out int v2) && v2 < i;
        }

        public static bool Lte(this Dictionary<string, string> pars, string key, int i)
        {
            if (pars == default)
            {
                return false;
            }

            return pars.TryGetValue(key, out string? value) && int.TryParse(value, out int v2) && v2 <= i;
        }

        public static bool Ne(this Dictionary<string, string> pars, string key, int i)
        {
            if (pars == default)
            {
                return false;
            }

            return pars.TryGetValue(key, out string? value) && int.TryParse(value, out int v2) && v2 != i;
        }
        #endregion


        public static bool Eq(this Dictionary<string, string> pars, string key, long i)
        {
            if (pars == default)
            {
                return false;
            }

            return pars.TryGetValue(key, out string? value) && long.TryParse(value, out long v2) && v2 == i;
        }

        public static bool Gt(this Dictionary<string, string> pars, string key, long i)
        {
            if (pars == default)
            {
                return false;
            }

            return pars.TryGetValue(key, out string? value) && long.TryParse(value, out long v2) && v2 > i;
        }

        public static bool Gte(this Dictionary<string, string> pars, string key, long i)
        {
            if (pars == default)
            {
                return false;
            }

            return pars.TryGetValue(key, out string? value) && long.TryParse(value, out long v2) && v2 >= i;
        }

        public static bool Lt(this Dictionary<string, string> pars, string key, long i)
        {
            if (pars == default)
            {
                return false;
            }

            return pars.TryGetValue(key, out string? value) && long.TryParse(value, out long v2) && v2 < i;
        }

        public static bool Lte(this Dictionary<string, string> pars, string key, long i)
        {
            if (pars == default)
            {
                return false;
            }

            return pars.TryGetValue(key, out string? value) && long.TryParse(value, out long v2) && v2 <= i;
        }

        public static bool Ne(this Dictionary<string, string> pars, string key, long i)
        {
            if (pars == default)
            {
                return false;
            }

            return pars.TryGetValue(key, out string? value) && long.TryParse(value, out long v2) && v2 != i;
        }

    }

}
